function data=loadGridFile(file)

% This file is part of CISNE.
%
%     CISNE is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     any later version.
%
%     CISNE is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with CISNE.  If not, see <http://www.gnu.org/licenses/>.

% Copyright 2011. Jose Maria Garcia-Valdecasas Bernal

% 21-Oct-09 ver. 1.0 (splitted from CISNE.m).


%check file type and how to load the data
fh1=fopen(file);
line=fgetl(fh1);
fclose(fh1);
if strcmp(line,'DSAA') %surfer ASCII Grid file
    [data xmin xmax ymin ymax]=readGrd(file);
    
%     set(handles.TL_BatX0,'String',num2str(xmin));
%     set(handles.TL_BatY0,'String',num2str(ymin));
%     [m,n]=size(matrix);
%     set(handles.TL_BatNX,'String',n);
%     set(handles.TL_BatNY,'String',m);
%     dx=(xmax-xmin)/(n-1);
%     dy=(ymax-ymin)/(m-1);
%     
%     set(handles.TL_BatDX,'String',dx);
%     set(handles.TL_BatDY,'String',dy);
%     
%     handles.bathymetry=matrix;
%     handles.batMode=2; %rows do not have to start in new lines
%     handles.batHeaders=5;
%     set(handles.TL_Header,'String','5')
    
else %let's suppose that we are handling a delimited matrix
    data=dlmread(file);
    
    if (~isempty(data))
        %[m,n]=size(matrix);
        %set(handles.TL_BatNX,'String',n);
        %set(handles.TL_BatNY,'String',m);
        
        %handles.bathymetry=matrix;
        %handles.batMode=1; %rows start in new lines
        %handles.batHeaders=0;
    else
        %we have some header lines and or continuos data lines. We need
        %more info to load the matrix. Here's the right way. TODO
    end
end

function [matrix xmin xmax ymin ymax]=readGrd(namefile)

grdfile=fopen(namefile,'r');    % Open file
fgetl(grdfile);            % Reads surfer code 'DSAA'
% Grid dimensions (number of nodes)
aux=str2num(fgetl(grdfile)); nx=aux(1); ny=aux(2); %#ok<ST2NM>
% X limits
aux=str2num(fgetl(grdfile)); xmin=aux(1); xmax=aux(2); %#ok<ST2NM>
% Y limits
aux=str2num(fgetl(grdfile)); ymin=aux(1); ymax=aux(2); %#ok<ST2NM>
% Z limits (ignored as we do not need them)
fgetl(grdfile);

% Add a progress-bar to left side of standard MJStatusBar container
set(handles.jProgressBar,'StringPainted','on','Minimum',0, 'Maximum',ny, 'Value',0);
handles.statusbarObj.setText('Loading bathymetry file...');
handles.jProgressBar.setValue(0);
handles.jProgressBar.setVisible(1);
% Read matrix
matrix=zeros(ny,nx);
for jj=1:ny
            
    row=textscan(grdfile,'%f',nx);
    matrix(jj,:)=cell2mat(row);
    %update progress bar to avoid loosing patience
   
    handles.jProgressBar.setValue(jj);
end

handles.jProgressBar.setVisible(0);
handles.statusbarObj.setText('Bathymetry succesfully loaded');
fclose(grdfile);